﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Ink_Master.Bots.InkMaster.Actions;
using Styx;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

#endregion

namespace Ink_Master.Bots.InkMaster.BagHandlers
{
    public class Search
    {
        /// <summary>
        ///   Gets the ink qty.
        /// </summary>
        /// <param name = "inkname">The inkname.</param>
        /// <returns></returns>
        public static int GetInkQty(string inkname)
        {
            return StyxWoW.Me.BagItems.Where(item => item.Name == inkname).Aggregate(0,
                                                                                     (current, item) =>
                                                                                     current + (Int32) item.StackCount);
        }

        /// <summary>
        ///   Gets the pigments.
        /// </summary>
        /// <returns></returns>
        public static List<WoWItem> GetPigments()
        {
            ObjectManager.Update();
            Log.Slog("Entered GetPigments Proc");
            List<uint> listofpigments = Pigments.PigmentList;
            return
                ObjectManager.Me.BagItems.Where(
                    pigment => listofpigments.Contains(ItemIdFromItemLink(pigment.ItemLink)) && pigment.StackCount >= 2)
                    .ToList();
        }

        /// <summary>
        ///   Gets the herbs.
        /// </summary>
        /// <returns></returns>
        public static List<WoWItem> GetHerbs()
        {
            ObjectManager.Update();
            Log.Slog("Entered GetHerbs proc.");
            List<uint> listOfHerbs = Herbs.HerbList();
            return
                ObjectManager.Me.BagItems.Where(
                    herb => listOfHerbs.Contains(ItemIdFromItemLink(herb.ItemLink)) && herb.StackCount >= 5).ToList();
        }

        public static uint ItemIdFromItemLink(string itemLink)
        {
            // |cff0070dd|Hitem:43666:0:0:0:0:0:0:0:85:0|h[Hellfire Tome]|h|r
            string temp = itemLink;
            Regex regex = new Regex(":[0-9]+:");
            string temp2 = regex.Match(temp).ToString();
            temp = temp2.Replace(":", "");
            return Convert.ToUInt32(temp);
        }
    }
}